home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 1.iso / toolbox / src / tutorials / custEducation / opengl2 / examples / accum / anti.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-11-11  |  4.9 KB  |  218 lines

  1. /*
  2.  * Copyright 1993, 1995, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17.  
  18. /* anti.c
  19.  * This program draws two intersecting cubes and uses the 
  20.  * accumulation buffer to do scene antialiasing.
  21.  *
  22.  *    <a> key            - toggle scene antialiasing
  23.  *    Escape key        - exit the program
  24.  *
  25.  * Note: this program must be compiled with accpersp.c and jitter.h
  26.  */
  27. #include <GL/gl.h>
  28. #include <GL/glu.h>
  29. #include <GL/glut.h>
  30.  
  31. #include <stdio.h>
  32. #include <math.h>
  33.  
  34. #include "jitter.h"
  35.  
  36. /*  Function Prototypes  */
  37.  
  38. GLvoid  initgfx( GLvoid );
  39. GLvoid  drawScene( GLvoid );
  40. GLvoid  reshape( GLsizei, GLsizei );
  41. GLvoid  animate( GLvoid );
  42. GLvoid  visibility( GLint );
  43. GLvoid  keyboard( GLubyte, GLint, GLint );
  44.  
  45. GLvoid toggleJitter(GLvoid); 
  46.  
  47. GLvoid printHelp( char * );
  48.  
  49. GLvoid accFrustum(GLdouble, GLdouble, GLdouble, GLdouble,
  50.         GLdouble, GLdouble, GLdouble, GLdouble, 
  51.         GLdouble, GLdouble, GLdouble);
  52. GLvoid accPerspective(GLdouble, GLdouble, GLdouble, GLdouble,
  53.         GLdouble, GLdouble, GLdouble, GLdouble, GLdouble);
  54.  
  55. /* Global Definitions */
  56.  
  57. #define KEY_ESC    27    /* ascii value for the escape key */
  58.  
  59. /* Global Variables */
  60.  
  61. static GLint        maxJitters = 1;
  62. static GLdouble        aspect = 1.0;
  63.  
  64. static GLfloat         angle = 0.0;
  65.  
  66. GLvoid
  67. main(int argc, char *argv[] )
  68. {
  69.     GLsizei     width, height;
  70.  
  71.     glutInit( &argc, argv );
  72.  
  73.     width = glutGet( GLUT_SCREEN_WIDTH ); 
  74.     height = glutGet( GLUT_SCREEN_HEIGHT );
  75.     glutInitWindowPosition( width/4, height/4 ); 
  76.     glutInitWindowSize( width/4, height/4 );
  77.     glutInitDisplayMode( GLUT_RGBA|GLUT_DEPTH|GLUT_DOUBLE|GLUT_ACCUM );
  78.     glutCreateWindow( argv[0] );
  79.     
  80.     initgfx();
  81.  
  82.     glutKeyboardFunc( keyboard );
  83.     glutReshapeFunc( reshape );
  84.     glutIdleFunc( animate ); 
  85.     glutVisibilityFunc( visibility ); 
  86.     glutDisplayFunc( drawScene ); 
  87.  
  88.     printHelp( argv[0] );
  89.  
  90.     glutMainLoop();
  91. }
  92.  
  93. GLvoid
  94. printHelp( char *progname )
  95. {
  96.     fprintf(stdout, "\n%s - use the accumulation buffer "
  97.         "to antialias an entire scene.\n\n"
  98.         "Expect it to run very slowly on systems without \n"
  99.         "a hardware accumulation buffer.\n\n"
  100.         "Escape key        - exit the program\n"
  101.         "<a> key        - toggle scene antialiasing\n\n", 
  102.         progname );
  103.  
  104.     printf("scene antialiasing is OFF\n");
  105. }
  106.  
  107. GLvoid
  108. initgfx(GLvoid)
  109. {
  110.     GLfloat shiny_mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };
  111.     GLfloat shiny_mat_shininess[] = { 128.0 };
  112.     
  113.     glMaterialfv(GL_FRONT, GL_SPECULAR, shiny_mat_specular); 
  114.     glMaterialfv(GL_FRONT, GL_SHININESS, shiny_mat_shininess); 
  115.     
  116.     glEnable(GL_DEPTH_TEST);
  117.  
  118.     glShadeModel(GL_FLAT);
  119.  
  120.     glEnable(GL_LIGHT0);
  121.     glEnable(GL_LIGHTING);
  122.  
  123.     glColorMaterial(GL_FRONT, GL_DIFFUSE);
  124.     glEnable(GL_COLOR_MATERIAL);
  125.  
  126.     glClearColor( 0.0, 0.0, 0.0, 1.0 );
  127.     glClearAccum( 0.0, 0.0, 0.0, 1.0 );
  128.  
  129.     glEnable(GL_DEPTH_TEST);
  130. }
  131.  
  132. GLvoid  
  133. toggleJitter(GLvoid)
  134. {
  135.     maxJitters = ( maxJitters == 1 ? 8 : 1 );
  136.  
  137.     printf("scene antialiasing is %s\n", (maxJitters==1?"OFF":"ON"));
  138. }
  139.  
  140. GLvoid 
  141. keyboard( GLubyte key, GLint x, GLint y )
  142. {
  143.     switch (key) {
  144.     case 'a':    /* toggle jittering */
  145.         toggleJitter();
  146.         glutPostRedisplay();
  147.         break;
  148.     case KEY_ESC:    /* Exit when the Escape key is pressed */
  149.         exit(0);
  150.     }
  151. }
  152.  
  153. GLvoid
  154. reshape( GLsizei width, GLsizei height )
  155. {
  156.     glViewport( 0, 0, width, height );
  157.  
  158.     aspect = (GLdouble) width/(GLdouble) height;
  159.  
  160.         glMatrixMode( GL_PROJECTION );
  161.         glLoadIdentity();
  162.         glMatrixMode( GL_MODELVIEW );
  163. }
  164.  
  165. GLvoid
  166. animate( GLvoid )
  167. {
  168.     angle = fmodf( (angle + 5.0), 360.0 );    /* update angle */
  169.     glutPostRedisplay();            /* Tell GLUT to redraw */
  170. }
  171.         
  172. GLvoid
  173. visibility( int state )
  174. {
  175.     if (state == GLUT_VISIBLE) {
  176.         glutIdleFunc( animate );
  177.     } else {
  178.         glutIdleFunc( NULL );
  179.     }
  180. }
  181.  
  182. /* Draws two cubes with scene antialiasing */
  183. GLvoid
  184. drawScene(GLvoid)
  185. {
  186.     int jitter;
  187.     
  188.     if ( maxJitters > 1 ) glClear(GL_ACCUM_BUFFER_BIT);
  189.  
  190.     for (jitter = 0; jitter < maxJitters; jitter++)
  191.     {
  192.         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  193.  
  194.         glPushMatrix();
  195.                 accPerspective(30.0, aspect, 1.0, 15.0, 
  196.                 j8[jitter].x, j8[jitter].y, 0.0, 0.0, 1.0);
  197.             glTranslatef (0.0, 0.0, -8.0); 
  198.  
  199.             glRotatef(angle, 1.0, 0.0, 0.0);
  200.      
  201.             glColor3f(0.9, 0.9, 0.9);
  202.             glutSolidCube( 1.0 );
  203.  
  204.             glRotatef(45.0, 0.0, 1.0, 0.0); 
  205.             glRotatef(45.0, 0.0, 0.0, 1.0);
  206.      
  207.             glColor3f(0.9, 0.0, 0.9);
  208.             glutSolidCube( 1.0 );
  209.         glPopMatrix();
  210.  
  211.         if ( maxJitters > 1 ) glAccum(GL_ACCUM,  1.0/maxJitters);
  212.     }
  213.  
  214.     if ( maxJitters > 1 ) glAccum(GL_RETURN,  1.0);
  215.  
  216.     glutSwapBuffers();
  217. }
  218.